package com.primaryAlgorithm;

import java.util.*;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-09-29 11:21:36
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-09-29     张李鑫                     1.0         1.0 Version
 */
public class IsAnagram {

    public static boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        char[] chars = s.toCharArray();
        char[] chars1 = t.toCharArray();
        HashMap<Object, Integer> map = new HashMap<>();
        HashMap<Object, Integer> map1 = new HashMap<>();

        for (int i = 0; i < chars.length; i++) {
            map.put(chars[i], map.getOrDefault(chars[i], 0) + 1);
            map1.put(chars1[i], map1.getOrDefault(chars1[i], 0) + 1);
        }

        Set<Map.Entry<Object, Integer>> entries = map1.entrySet();
        Set<Map.Entry<Object, Integer>> entries1 = map.entrySet();
        Iterator<Map.Entry<Object, Integer>> iterator1 = entries1.iterator();
        Iterator<Map.Entry<Object, Integer>> iterator = entries.iterator();
        while (iterator1.hasNext() && iterator.hasNext()) {
            if (iterator.next().getKey().equals(iterator1.next().getKey())) {
                if (iterator.next().getValue().equals(iterator1.next().getValue())){
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String s = "rat", t = "car";
        boolean anagram = isAnagram(s, t);
        System.out.println(anagram);
    }
}
